// 初始化源数据库的副本集和测试数据
print('Initializing source MongoDB...');

// 等待数据库启动
sleep(5000);

// 初始化副本集
try {
  rs.initiate({
    _id: "rs0",
    members: [
      { _id: 0, host: "localhost:27017" }
    ]
  });
  print('Replica set initialized successfully');
} catch (error) {
  print('Replica set already initialized or error:', error.message);
}

// 等待副本集选举完成
sleep(3000);

// 创建测试数据库和集合
db = db.getSiblingDB('testdb');

// 创建测试集合
db.createCollection('users');
db.createCollection('products');
db.createCollection('orders');

// 插入测试数据
db.users.insertMany([
  { _id: 1, name: "Alice", email: "alice@example.com", age: 25 },
  { _id: 2, name: "Bob", email: "bob@example.com", age: 30 },
  { _id: 3, name: "Charlie", email: "charlie@example.com", age: 35 }
]);

db.products.insertMany([
  { _id: 1, name: "Laptop", price: 999.99, category: "Electronics" },
  { _id: 2, name: "Mouse", price: 29.99, category: "Electronics" },
  { _id: 3, name: "Keyboard", price: 89.99, category: "Electronics" }
]);

db.orders.insertMany([
  { _id: 1, userId: 1, productId: 1, quantity: 1, total: 999.99, date: new Date() },
  { _id: 2, userId: 2, productId: 2, quantity: 2, total: 59.98, date: new Date() }
]);

print('Test data inserted successfully');
print('Source database initialization completed'); 